package com.qdsg.ysg.doctor.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.multidex.BuildConfig;
import androidx.multidex.MultiDexApplication;

import com.ainemo.sdk.otf.NemoSDK;
import com.ainemo.sdk.otf.NemoSDKInitCallBack;
import com.ainemo.sdk.otf.Settings;
import com.ainemo.sdk.otf.VideoConfig;
import com.qdsg.ysg.doctor.eventbus.WebSocket;
import com.qdsg.ysg.doctor.util.DebuggerUtils;
import com.qdsg.ysg.doctor.util.UIConstants;
import com.rest.response.DoctorResponse;
import com.rest.response.PrescriptionListResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jz.joyoung.robot.util.LogUtil;


/**
 * Created by Administrator on 2018/1/9.
 */

public class BaseApplication extends MultiDexApplication {

    public static int userType; //1:基层医生 2：中心医院医生
    public static String patientUserId = "";
    public static String diagnoseId = "";
    public static String currentUserId;
    public static String doctorId; //当前医生id
    public static String name;
    public static String jobTitle;
    public static String avatar;
    public static String patientAvatar;
    public static String sex;
    public static String patientSex;
    public static String conferenceID;
    public static int isAudit; //0 医生 1药剂师  2护士
    public static Map<String, WebSocket> runningCalls = new HashMap<>();

    public static String xyAccount = "";//小鱼账号
    public static String xyPassword = "";//小鱼账号密码
    public static Boolean isXyLogin = false;

    private static BaseApplication context;

    public static DoctorResponse.Doctor currentDoctor;

    public static HashMap<String, PrescriptionListResponse.PrescriptionWrapper> prescriptionHashMap = new HashMap<>();

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
//        DebuggerUtils.checkDebuggableInNotDebugModel(context);

//        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
//            @Override
//            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
//                LogUtil.d("__activity-onActivityCreated", activity.getClass().getSimpleName());
//            }
//
//            @Override
//            public void onActivityStarted(@NonNull Activity activity) {
//
//            }
//
//            @Override
//            public void onActivityResumed(@NonNull Activity activity) {
//                Log.d("TAG_APP", activity.getClass().getSimpleName() + "_taskId:" + activity.getTaskId());
//            }
//
//            @Override
//            public void onActivityPaused(@NonNull Activity activity) {
//
//            }
//
//            @Override
//            public void onActivityStopped(@NonNull Activity activity) {
//
//            }
//
//            @Override
//            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
//
//            }
//
//            @Override
//            public void onActivityDestroyed(@NonNull Activity activity) {
//                LogUtil.d("__activity-onActivityDestroyed", activity.getClass().getSimpleName());
//
//            }
//        });
//        initXySDK();
    }

    /**
     * 小鱼sdk初始化
     */
    private void initXySDK() {
        Settings settings = new Settings("3e816492058911e7a31d000c29971af5");
        settings.setPrivateCloudAddress("39.172.69.238:7443");
        settings.setUiNeedSpeakers(true);
        settings.setVideoMaxResolutionTx(VideoConfig.VD_1280x720);
        settings.setDefaultCameraId(1);
        NemoSDK.getInstance().init(this, settings, new NemoSDKInitCallBack() {
            @Override
            public void nemoSdkInitSuccess() {
                LogUtil.e("小鱼SDK初始化成功");
            }

            @Override
            public void nemoSdkInitFail(String s, String s1) {
                LogUtil.e("SDK初始化失败");
            }
        });
    }

    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new ClassicsHeader(context));
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new BallPulseFooter(context).setSpinnerStyle(SpinnerStyle.Scale));
    }

    public static Context getContext() {
        return context;
    }

//    /**
//     * 初始化推送SDK，在用户隐私政策协议同意后，再做初始化
//     */
//    private void initPushSDK() {
//        /*
//         * 判断用户是否已同意隐私政策
//         * 当同意时，直接进行初始化；
//         * 当未同意时，待用户同意后，通过PushHelper.init(...)方法进行初始化。
//         */
//        boolean agreed = MyPreferences.getInstance( this ).hasAgreePrivacyAgreement();
//        if (agreed) {
//            //建议在线程中执行初始化
//            new Thread( () -> PushHelper.init( getApplicationContext() ) ).start();
//        }
//    }

    private static boolean isFrontProcess(Context context, String frontPkg) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

        assert manager != null;
        List<ActivityManager.RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
        if (infos == null || infos.isEmpty()) {
            return false;
        }

        final int pid = android.os.Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            if (info.pid == pid) {
                Log.i(UIConstants.DEMO_TAG, "processName-->" + info.processName);
                return frontPkg.equals(info.processName);
            }
        }

        return false;
    }

    /**
     * 检测动态调试
     */
    public void detectedDynamicDebug() {
        if (!BuildConfig.DEBUG) {
            if (Debug.isDebuggerConnected()) {
                //进程自杀
                int myPid = android.os.Process.myPid();
                android.os.Process.killProcess(myPid);

                //异常退出虚拟机
                System.exit(1);
            }
        }
    }

}

